package com.wanxi.controller;

import com.wanxi.model.StudentModel;

import java.util.Arrays;
import java.util.Scanner;

public class StudentController {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int size = scanner.nextInt();
        new StudentController().printStudentModels(size);

    }

    private void printStudentModels(int size) {

        StudentModel[] studentModels = new StudentModel[3];

//        StudentModel[] models = new StudentModel[4];
//        丽丽, 22?思考？有没有更加简单的方式？Java有没有API？已经写好的代码直接调用？
        Scanner scanner = new Scanner(System.in);

//        将学生信息封装到数组
        for (int i = 0; i < studentModels.length; i++) {
            System.out.println("请输入学生信息，格式如：张三,23");
            String str = scanner.next();
//        将输入的信息封装到model，所以应该单独写一个方法
            StudentModel model = setStudentModel(str);
            studentModels[i] = model;
        }

//        将学生信息打印到控制台
        for (int i = 0; i < studentModels.length; i++) {
            System.out.println("学生姓名为：" + studentModels[i].getName() + " 年龄为：" + studentModels[i].getAge());
        }
        System.out.println(Arrays.toString(studentModels));
    }

    /**
     * 将得到控制台的字符串解析为学生对象
     * 张三,23
     *
     * @param str
     * @return
     */
    private static StudentModel setStudentModel(String str) {
        StudentModel model = new StudentModel();
        model.setName(str.split(",")[0]);
        String ageStr = str.split(",")[1];
        int age = Integer.parseInt(ageStr);
        model.setAge(age);
        return model;
    }
}
